package com.hedl.practice.diyJava.gather.study_stack.practice.test;

import com.hedl.practice.diyJava.gather.study_stack.practice.tmp.SuffixExpression;

import java.util.*;

public class PostfixExpression {
    //利用Stack把字符串中缀表达式编译为后缀表达式，然后再利用栈执行后缀表达式获得计算结果：
    public static void main(String[] args) {
        String exp = "1+2*(9-5)";
        SuffixExpression se = compile(exp);
        int result = se.execute();
        System.out.println(exp+"="+result+""+(result == 1+2*(9-5)?"✓" : "✗"));
    }


    static SuffixExpression compile(String exp){
        Deque<String> figure = new LinkedList<>();
        Deque<String> character = new LinkedList<>();
        for(int i=0;i < exp.length();i++) {
            String n = exp.charAt(i)+"";
            if (n.equals("(") || n.equals(")"))continue;;
            if (n.equals("+") || n.equals("-") || n.equals("*") || n.equals("/")){
                character.push(n);
                continue;
            }
            figure.push(n);

        }
        System.out.println(figure);
        System.out.println(character);
        return new SuffixExpression(figure,character);
    }
}
